<template>
  <div>
    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel @formChange="changeHandle" ref="formPanel" labelWidth="100" :list="formList" :cols="3" :isSubmitBtn="false" />
      </div>
    </el-col>
    <el-col :span="24" class="bg1" v-if="dataSource.length > 0">
      <BreakSpace label="文件预览" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
      <FilterTable height="200" ref="table2" :columns="columns2" :dataSource="dataSource" :isSelectColumn="false" :onColumnsChange="columns => (this.columns2 = columns)"> </FilterTable>
    </el-col>

    <el-col :span="24" class="bg1">
      <BreakSpace label="审核记录" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
      <FilterTable height="200" ref="tableHistory" :columns="columns" :isSelectColumn="false" :onColumnsChange="columns => (this.columns = columns)"> </FilterTable>
    </el-col>

    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel ref="formPanel2" :list="formList2" :cols="3" :isSubmitBtn="false" />
      </div>
    </el-col>
    <div class="dialogBottmView">
      <!--<multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this,1)">审核通过</multiuse-button>-->
      <multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this, 1)">驳回</multiuse-button>
      <multiuse-button type="primary" class="btnStyle" :click="closeMenu.bind(this, 2)">关闭</multiuse-button>
    </div>
  </div>
</template>

<script>
import { downFile, addVehicleWarranty, getWarranty, getVehicle, blackAudit } from '@/api/repair/newEnergyVehicleLifetime/vehicleLifetimeReviewCDuan';

import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { dictionary } from '@/utils/dictMixin';
import { isEmpty } from '@/utils/utils';
import { exportFile } from '@/utils/index';
import { getAppId } from '@/assets/js/auth';

export default {
  name: 'detail',
  mixins: [dictionary, dealerMixin],
  props: ['handleType', 'warrantyId'],
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      formList2: this.createFormList2(),
      columns: this.createTableColumns(),
      columns2: this.createTableColumns2(),
      fetchApi: addVehicleWarranty,
      fetchParams: { xhrAbort: true },
      visibleVin: false,
      fileObj: {},
      dataSource: []
    };
  },
  components: {},
  mounted() {
    this.BaseTable = this.$refs.tableHistory.$refs.pageTable;
    if (getAppId() == 'arcfox') {
      this.formList = this.createFormListArcfox();
    } else {
      this.formList = this.createFormList();
    }
    this.getAllUsers(this.columns, ['createdBy'], 'table');

    this.getData();
  },
  methods: {
    changeHandle(val) {
      this.saveData = val;
    },
    async seacheFun() {
      // this.visibleVin = true

      let vin = this.formList.find(item => item.fieldName == 'vin').initialValue;

      if (isEmpty(vin)) {
        this.$notify.warning({ title: '提示信息', message: 'vin不能为空' });
        return;
      }

      let res = await getVehicle({ VIN: vin });

      if (res.resultCode == 200 && res.success == true) {
        let data = isEmpty(res['data']) ? {} : res['data'];

        this.selectFun([data]);
      }
    },
    closeDialog() {
      this.visibleVin = false;
    },
    selectFun(arr) {
      this.closeDialog();

      for (let i in arr[0]) {
        this.formList.map(x => {
          if (i == x.fieldName) {
            x.initialValue = arr[0][i];
          }
          if (x.fieldName == 'certType') {
            x.initialValue = Number(arr[0]['certType']);
          }
          if (x.fieldName == 'vehiclePurpose') {
            x.initialValue = Number(arr[0]['vehiclePurpose']);
          }
        });
      }
    },

    createTableColumns2() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '文件名称',
          dataIndex: 'fileName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '文件路径',
          dataIndex: 'path',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '文件类型',
          dataIndex: 'fileType',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '预览',
          dataIndex: 'name',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return (
              <div style="width: 100px" onClick={() => this.downloadFun(props.row)}>
                <el-button size="mini" type="text" icon="el-icon-download">
                  下载预览
                </el-button>
              </div>
            );
          }
        }
      ];
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          dictItems: [],
          filterItems: [],
          filterType: 'checkbox'
        },
        {
          title: '操作时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '操作后状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1022),
          render: props => {
            return <span>{this.createDictText(props.row.status, 1022)}</span>;
          }
        },
        {
          title: '操作备注',
          dataIndex: 'remarks',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    /*根据ID查询*/
    async getData() {
      let res = await getWarranty({ warrantyId: this.warrantyId });

      if (res.resultCode == 200 && res.success == true) {
        let data = isEmpty(res['data']) ? {} : res['data'];

        let ttRpVehicleWarrantyLogPOList = isEmpty(data['ttRpVehicleWarrantyLogPOList']) ? [] : data['ttRpVehicleWarrantyLogPOList'];

        this.BaseTable.EXECUTE_INSERT(ttRpVehicleWarrantyLogPOList);
        //
        // let driveLicenseName = isEmpty(data['driveLicenseName']) ? '' : data['driveLicenseName'];
        // let driveLicenseImg = isEmpty(data['driveLicenseImg']) ? '' : data['driveLicenseImg'];
        // let fileType = driveLicenseName.substring(driveLicenseName.indexOf('.') + 1).toLowerCase();
        // let obj = {
        //   fileName: driveLicenseName,
        //   path: driveLicenseImg,
        //   fileType: fileType
        // };
        // // this.dataSource = [obj]
        // let fileObj = {
        //   name: driveLicenseName,
        //   url: driveLicenseImg
        // };
        //
        // this.formList.find(item => item.fieldName == 'url').initialValue = [fileObj];
        data.fileList.map(x => {
          x.name = x.title;
          x.url = x.fileUrl;
        });
        this.formList.find(item => item.fieldName == 'url').initialValue = data.fileList;

        for (let i in data) {
          this.formList.map(x => {
            if (i == x.fieldName) {
              x.initialValue = data[i];
              if (x.fieldName == 'vin') {
                x.disabled = true;
              }
              if (x.fieldName == 'certType') {
                x.initialValue = Number(data['certType']);
              }
              if (x.fieldName == 'vehiclePurpose') {
                x.initialValue = Number(data['vehiclePurpose']) == 0 ? null : Number(data['vehiclePurpose']);
              }
            }
          });
        }
      }
    },

    async saveOrSubmit(type) {
      let urlObj = this.formList.find(item => item.fieldName == 'url').initialValue[0];
      if (isEmpty(urlObj)) {
        urlObj = {};
      }
      let path = isEmpty(urlObj.url) ? '' : urlObj.url;
      let fileName = isEmpty(urlObj.name) ? '' : urlObj.name;
      this.$refs.formPanel.SUBMIT_FORM();
      let obj = this.saveData;
      obj.driveLicenseImg = path;
      obj.driveLicenseName = fileName;
      /*10221003 保存  提交 10221001*/
      if (this.handleType == 'add') {
        obj.warrantyId = '';
      } else {
        obj.warrantyId = this.warrantyId;
      }
      if (1 == type) {
        //保存
        obj.status = '10221003';
      } else {
        //提交
        obj.status = '10221001';
      }

      let res = await addVehicleWarranty(obj);

      if (res.resultCode == 200 && res.success == true) {
        this.$notify.success({ title: '提示信息', message: '操作成功' });

        this.$emit('close', 'refresh');
      }
    },

    async closeMenu(type) {
      if (1 == type) {
        let remarks = this.formList2.find(item => item.fieldName == 'remarks').initialValue;

        if (isEmpty(remarks)) {
          this.$notify.warning({ title: '提示信息', message: '驳回原因不能为空' });
          return;
        }

        let params = {
          remarks: remarks,
          status: '10221002',
          warrantyId: this.warrantyId
        };

        let res = await blackAudit(params);

        if (res.resultCode == 200 && res.success == true) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });

          this.$emit('close', 'refresh');
        }
      } else {
        this.$emit('close');
      }
    },

    downloadFun(row) {
      this.$confirm('是否文件保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          downFile({ filePath: row['path'] })
            .then(res => {
              console.log(res, 'res');
              exportFile(res.data, row['fileName'], '');
            })
            .catch(err => {});
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },

    createFormList2() {
      return [
        {
          type: 'TEXT_AREA',
          label: '审核原因',
          selfCols: 3,
          fieldName: 'remarks',
          disabled: false,
          placeholder: '',
          rules: [{ required: true, trigger: 'change', message: '请输入审核原因' }],
          initialValue: ''
        }
      ];
    },

    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          disabled: true,
          placeholder: '',
          readonly: false,
          rules: [{ required: true, trigger: 'change', message: '请选择vin' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'model',
          placeholder: '',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '配置名称',
          fieldName: 'apackage',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'innerColor',
          placeholder: '请选择',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '外色',
          fieldName: 'color',
          placeholder: '请选择',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '整车编码',
          fieldName: 'counts35555',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '电机号',
          fieldName: 'motorNo',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '当前行驶里程',
          fieldName: 'currentMileage',
          placeholder: '请选择',
          labelWidth: '110px',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },

        {
          type: 'BREAK_SPACE',
          label: '客户信息'
        },

        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'ownerName',
          disabled: true,
          placeholder: '',
          // rules: [{required: true, trigger: 'change'}],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车主电话',
          fieldName: 'mobile',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'certType',
          placeholder: '',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: '',
          itemList: this.createDictList(8013)
        },
        {
          type: 'INPUT',
          label: '证件号码',
          fieldName: 'certNo',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '购车用途',
          fieldName: 'vehiclePurpose',
          placeholder: '请选择',
          disabled: true,
          initialValue: '',
          itemList: this.createDictList('1511')
        },
        {
          type: 'DATE',
          label: '保修起始日期',
          fieldName: 'salesDate',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '联系地址',
          fieldName: 'address',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'DATE',
          label: '行驶证注册日期',
          fieldName: 'driveLicenseDate',
          format: 'yyyy-MM-dd',
          placeholder: '请选择',
          disabled: true,
          labelWidth: '110px',
          rules: [{ required: true, trigger: 'change', message: '请选择行驶证注册日期' }],
          initialValue: ''
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传',
          fieldName: 'url',
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            fileTypes: ['jpg', 'png', 'pdf', 'rar'],
            max: 1024 * 100,
            limit: 1
          },
          initialValue: '',
          disabled: true,
          readonly: true,
          selfCols: 1,
          rules: [{ required: false, message: '请上传文件', trigger: 'blur' }]
        }
      ];
    },
    createFormListArcfox() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          disabled: true,
          placeholder: '',
          readonly: false,
          rules: [{ required: true, trigger: 'change', message: '请选择vin' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'model',
          placeholder: '',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '配置名称',
          fieldName: 'apackage',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'innerColor',
          placeholder: '请选择',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '外色',
          fieldName: 'color',
          placeholder: '请选择',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '整车编码',
          fieldName: 'counts35555',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '电机号',
          fieldName: 'motorNo',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '当前行驶里程',
          fieldName: 'currentMileage',
          placeholder: '请选择',
          labelWidth: '110px',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },

        {
          type: 'BREAK_SPACE',
          label: '客户信息'
        },

        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'ownerName',
          disabled: true,
          placeholder: '',
          // rules: [{required: true, trigger: 'change'}],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车主电话',
          fieldName: 'mobile',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          placeholder: '请输入',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'certType',
          placeholder: '',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: '',
          itemList: this.createDictList(8013)
        },
        {
          type: 'INPUT',
          label: '证件号码',
          fieldName: 'certNo',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '购车用途',
          fieldName: 'vehiclePurpose',
          placeholder: '请选择',
          disabled: true,
          initialValue: '',
          itemList: this.createDictList('1511')
        },
        {
          type: 'DATE',
          label: '保修起始日期',
          fieldName: 'salesDate',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '联系地址',
          fieldName: 'address',
          placeholder: '请选择',
          disabled: true,
          rules: [{ required: false, trigger: 'change' }],
          initialValue: ''
        },
        {
          type: 'DATE',
          label: '行驶证注册日期',
          fieldName: 'driveLicenseDate',
          format: 'yyyy-MM-dd',
          placeholder: '请选择',
          disabled: true,
          labelWidth: '110px',
          rules: [{ required: true, trigger: 'change', message: '请选择行驶证注册日期' }],
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '质保类型',
          fieldName: 'warrantyType',
          placeholder: '请选择',
          initialValue: '',
          rules: [{ required: true, trigger: 'change', message: '请选择质保类型' }],
          itemList: this.createDictList('8888')
          // itemList: [
          //   { text: '整车', value: 88881001 },
          //   { text: '三电', value: 88881002 },
          //   { text: '整车和三电', value: 88881003 }
          // ]
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传',
          fieldName: 'url',
          upload: {
            actionUrl: '/api/file/oss/upload', //上传的地址
            fileTypes: ['jpg', 'png', 'pdf', 'rar'],
            max: 1024 * 100,
            limit: 1
          },
          initialValue: '',
          disabled: true,
          readonly: true,
          selfCols: 1,
          rules: [{ required: false, message: '请上传文件', trigger: 'blur' }]
        }
      ];
    }
  }
};
</script>

<style scoped lang="less">
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}

.minBtnStytle {
  text-align: center;
  background: #00feff;
  padding-left: 10px;
}

.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}

.btn {
  text-align: center;
}

.btnStyle {
  margin-top: 10px;
}
</style>
